﻿using System.Collections.Generic;

namespace FinalCountdownServices.Extensions
{
    public static class DictionaryExtensions
    {
        /// <summary>
        /// Beszúr új element aminek a key-e érték típusa (Type.FullName) lesz
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dict"></param>
        /// <param name="value"></param>
        public static void Set<T>(this IDictionary<string, object> dict, T value)
        {
            string key = typeof(T).FullName;
            dict[key] = value;
        }

        public static void Set(this IDictionary<string, object> dict, string key, object value)
        {
            dict[key] = value;
        }

        /// <summary>
        /// Lekérdez elemet aminek a key-e érték típusa (Type.FullName) lesz
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dict"></param>
        /// <param name="defvalue"></param>
        /// <returns></returns>
        public static T Get<T>(this IDictionary<string, object> dict, T defvalue = default(T))
        {
            object val;
            string key = typeof(T).FullName;
            if (dict.TryGetValue(key, out val) && val is T)
            {
                return (T)val;
            }
            else
                return defvalue;
        }


        public static T Get<T>(this IDictionary<string, object> dict, string key, T defvalue = default(T))
        {
            object val;
            if (dict.TryGetValue(key, out val) && val is T)
            {
                return (T)val;
            }
            else
                return defvalue;
        }
    }
}